<template>
  <div class="register-container">
    <div class="register-box">
      <h1 class="title">B海冷链生鲜直送</h1>
      <h2 class="subtitle">用户注册</h2>
      <a-form @submit.prevent="handleSubmit">
        <a-form-item>
          <a-input v-model:value="username" placeholder="用户名" />
        </a-form-item>
        <a-form-item>
          <a-input-password v-model:value="password" placeholder="密码" />
        </a-form-item>
        <a-form-item>
          <a-input-password v-model:value="confirmPassword" placeholder="重复密码" />
        </a-form-item>
        <a-form-item>
          <a-input v-model:value="phone" placeholder="电话号码" />
        </a-form-item>
        <a-form-item>
          <a-select v-model:value="role" placeholder="选择角色">
            <a-select-option value="customer">顾客</a-select-option>
            <a-select-option value="business">商家</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item>
          <a-button class="button-register" type="link" @click="handleBack">返回登录</a-button>
          <a-button class="button-register" type="primary" html-type="submit">注册</a-button>
        </a-form-item>
      </a-form>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { message } from 'ant-design-vue';
import request from '../util/request';

export default {
  name: 'Register',
  setup() {
    const router = useRouter();

    const username = ref('');
    const password = ref('');
    const confirmPassword = ref('');
    const phone = ref('');
    const role = ref('');

    const handleSubmit = () => {
      if (username.value === '' || password.value === '' || confirmPassword.value === '' || phone.value === '' || role.value === '') {
        message.info('请填写完整信息。');
        return;
      }
      if (password.value !== confirmPassword.value) {
        alert('两次输入的密码不匹配，请重新输入。');
        return;
      }
      console.log('用户名:', username.value);
      console.log('密码:', password.value);
      console.log('电话号码:', phone.value);
      console.log('角色:', role.value);
      // 这里可以添加注册的 API 调用逻辑
      request.post('/register', {
        username: username.value,
        password: password.value,
        phone: phone.value,
        role: role.value,
      }).then((response) => {
        console.log('注册:', response.data);
        // 根据注册结果进行相应的操作
        if (response.data.flag === "success") {
          message.success('注册成功，即将跳转到用户个人信息页面');
          setTimeout(() => {
            if (role.value === 'customer') {
              sessionStorage.setItem('userid', response.data.userid);
              sessionStorage.setItem('role', 'customer');
              router.push({ name: 'InfoViewer' });
            } else if (role.value === 'business') {
              sessionStorage.setItem('userid', response.data.userid);
              sessionStorage.setItem('role', 'business');
              router.push({ name: 'BusInfoViewer' });
            }
            message.info('请务必记住您的用户ID: ' + response.data.userid);
            message.info('用户登录将使用ID和密码进行登录');
          }, 1000);
        } else {
          message.error('注册失败, 请稍后重试或联系管理员');
        }
      }).catch((error) => {
        console.error('注册失败:', error);
        message.error('注册失败(' + error.message + ')');
      });
    };

    const handleBack = () => {
      // 跳转到登录页面
      router.push({ name: 'Login' });
    };

    return {
      username,
      password,
      confirmPassword,
      phone,
      role,
      handleSubmit,
      handleBack,
    };
  },
};
</script>

<style scoped>
/* @import '../assets/styles/login.css'; */

.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
}

.register-box {
  background-color: #fff;
  padding: 40px;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  width: 400px;
  text-align: center;
}

.title {
  font-size: 28px;
  margin-bottom: 10px;
  color: #1890ff;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
}

.subtitle {
  font-size: 20px;
  margin-bottom: 20px;
  color: #555;
}

.button-register {
  margin-right: 30px;
}
</style>